home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 February / macformat-047.iso / Shareware Plus / Utilities / ServerPower 1.0 / source / main.c next >
Encoding:
C/C++ Source or Header  |  1996-11-14  |  2.6 KB  |  119 lines  |  [TEXT/CWIE]

  1. // "main.c" - main entry point for ServerPower Extension.
  2. // Written November, 1996 by Dave Warker <davew@waterw.com>
  3. // Based on Tech Note 1079 - Power Management & Servers: Auto Restart from Power Failure.
  4.  
  5. #include "ShowInitIcon.h"
  6.  
  7.  
  8. /// - constants.
  9.  
  10. enum // ShowInitIcon() icons.
  11. {
  12.     rIconServerPower = 128,                // unit set to server mode
  13.     rIconNotSupported,                    // File Server mode not available on this Mac
  14.     rIconFailed                            // call to set server mode failed
  15. };
  16.  
  17.  
  18. /// - prototypes.
  19.  
  20. void main(void);
  21.  
  22.  
  23. /// - Cuda Manager definitions.
  24.  
  25. enum // commands.
  26. {
  27.     pseudoPkt        = 0x01,                // standard command type
  28.  
  29.     EnDisFileS        = 0x13,                // enable/disable File Server power mode
  30.     WakeupMode        = 0x23,                // enable/disable wakeup mode
  31.     GetPwrFailTime    = 0x27                // read time of last power failure
  32. };
  33.  
  34. typedef struct // parameter block for Egret trap.
  35. {
  36.     unsigned char    pbCmdType;            // command type (always 'pseudoPkt')
  37.     unsigned char    pbCmd;                // command
  38.     union                                // parameter
  39.     {
  40.         unsigned char    pByte[4];
  41.         unsigned short    pWord[2];
  42.         unsigned long    pLong;
  43.     }
  44.     pbParam;
  45.     unsigned short        pbByteCnt;        // number of bytes passed in buffer
  46.     unsigned char*        pbBufPtr;        // pointer to a buffer
  47.     unsigned char        pbFlags;        // flags returned by Cuda
  48.     unsigned char        pbSpare;        // (not used, reserved)
  49.     short                pbResult;        // result code from Cuda
  50.     ProcPtr                pbCompletion;    // routine to call on completion or 'nil'
  51. }
  52. CudaPB, *CudaPbPtr;
  53.  
  54. extern long cudaGlobalsPtr : 0xde0L;
  55.     enum { noCudaGlobals = -1L };
  56.     // Used to determine if Cuda code is loaded.
  57.  
  58. #pragma parameter Cuda(__A0)
  59. void Cuda(CudaPbPtr) = 0xA092;
  60.     // Cuda trap call.
  61.  
  62.  
  63. /// - implementation.
  64.  
  65. static Boolean ServerPowerModeIsAvailable(void)
  66. {
  67.     long result;
  68.     if (Gestalt(gestaltHardwareAttr,&result) == noErr && (result & (1L << gestaltHasSoftPowerOff)))
  69.     {
  70.         // Gestalt says soft-power off is available, let's check for Cuda!
  71.         if (GetOSTrapAddress(_EgretDispatch) != GetOSTrapAddress(_Unimplemented))
  72.         {
  73.             // Trap is there, but is the software loaded?
  74.             if (cudaGlobalsPtr != noCudaGlobals)
  75.                 // We have a winner!
  76.                 return true;
  77.         }
  78.     }
  79.     return false;
  80. }
  81.  
  82.  
  83. static OSErr SelectServerPowerMode(Boolean turnItOn)
  84. {
  85.     CudaPB pb;
  86.  
  87.     pb.pbCmdType        = pseudoPkt;
  88.     pb.pbCmd            = EnDisFileS;
  89.     pb.pbByteCnt        = 0;
  90.     pb.pbBufPtr            = nil;
  91.     pb.pbResult            = noErr;
  92.     pb.pbCompletion        = nil;
  93.  
  94.     pb.pbParam.pLong    = 0;
  95.     pb.pbParam.pByte[0]    = turnItOn ? 1 : 0;
  96.  
  97.     Cuda(&pb);
  98.  
  99.     return pb.pbResult;
  100. }
  101.  
  102.  
  103. void main(void)
  104. {
  105.     int icon;
  106.  
  107.     if (ServerPowerModeIsAvailable())
  108.     {
  109.         if (SelectServerPowerMode(true) == noErr)
  110.             icon = rIconServerPower;
  111.         else
  112.             icon = rIconFailed;
  113.     }
  114.     else
  115.         icon = rIconNotSupported;
  116.  
  117.     ShowInitIcon(icon, true);
  118. }
  119.